package com.atguigu.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.alipay.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author : Jungle
 * @Description :
 */

@Controller
public class PayWebController {
    @Autowired
    private AlipayTemplate alipayTemplate;
    @Autowired
    private OrderService orderService;

    /**
     * 这里返回的是页面显示
     * 声明产生的是html数据，不是json数据，需要标注
     *   1、将支付页面让浏览器展示
     *   2、支付成功以后，我们要跳到用户订单列表页
     */
    @ResponseBody
    @GetMapping(value = "/payOrder", produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn) {
        try {
            PayVo vo = orderService.getOrderPay(orderSn);
            //得到一个支付扫码页面，将此页面直接交给浏览器就可以
            String pay = alipayTemplate.pay(vo);
            return pay;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
}
